using Autofac;
using Autofac.Extensions.DependencyInjection;
using IoC.Interfaces;
using IoC.Services;
using NLog.Extensions.Logging;
using WebApi.Extensions;

namespace WebApi
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.

            // 注：必须要在appsettings.[ENV].json中将日志级别设置为Debug才能输出DEBUG日志
            // log4net配置
            //builder.Logging.AddLog4Net("CfgFiles/log4net.config");

            // NLog配置
            builder.Logging.AddNLog("CfgFiles/NLog.config");

            // 使用 Autofac
            builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
            builder.Host.ConfigureContainer<ContainerBuilder>(cb => cb.RegisterModule(new AutofacModule()));

            // 配置CORS 方式一
            //builder.Services.AddCors(options =>
            //{
            //    options.AddPolicy("cors", opt => opt.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
            //    //options.AddPolicy("cors", opt => opt.WithOrigins("http://a/", "http://b/").WithMethods(HttpMethod.Get.Method, "POST", "PUT", "DELETE", "OPTIONS"));
            //});
            //WebApplication app = builder.Build();
            //app.UseCors("cors");

            IServiceCollection services = builder.Services;
            services.AddControllers();
            // 配置 Swagger
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            services.AddSwaggerExt();

            //services.AddControllers().AddJsonOptions(options =>
            //{
            //    // 默认情况下，JSON的属性命名为Camel-case，可以设置成 Snake-case (a_b_c) 或 Kebab-case (a-b-c)，但不支持 Pascal-case
            //    //options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower;
            //});

            // 想支持 Pascal-case，需要引入 Microsoft.AspNetCore.Mvc.NewtonsoftJson 包，然后进行以下设置
            services.AddControllers().AddNewtonsoftJson(options =>
            {
                // Pascal-case
                //options.SerializerSettings.ContractResolver = new DefaultContractResolver();
                // Camel-case
                //options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

                // 设置 DateTime 序列化数据格式
                options.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat;
                // 设置 DateTime 格式化字符串
                //options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
            });

            // 是否配置 Autofac 并不影响使用原生的 IServiceCollection 进行注入配置
            //services.AddTransient<IMicroPhone, MicroPhone>();
            services.AddTransient<IPower, Power>();
            services.AddTransient<IHeadPhone, HeadPhone>();
            services.AddTransient<ICellPhone, ApplePhone>();

            WebApplication app = builder.Build();

            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.UseSwaggerExt();
            }

            // 配置 CORS 方式二
            app.UseCors(options =>
            {
                options.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
                // options.WithOrigins("http://a/", "http://b/").WithMethods("GET", "POST", "PUT", "DELETE");
            });

            app.UseHttpsRedirection();

            app.UseAuthorization();

            app.MapControllers();

            app.Run();
        }
    }
}